package com.hz.edu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hz.api.client.TeacherApi;
import com.hz.api.model.dto.TeacherDTO;
import com.hz.api.model.enums.ApiResponseCodeEnum;
import com.hz.api.model.request.ApiResponse;
import com.hz.edu.mapper.CourseMapper;
import com.hz.edu.model.entity.CourseEntity;
import com.hz.edu.service.CourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    TeacherApi teacherApi;

    @Resource
    CourseMapper courseMapper;

    @Override
    public int addCourse(String courseName, String courseCode,  Integer teacherId, Integer sectionCount,
                         String courseTime, Integer maxStudentCount, String courseProfile, String textbook,
                         Integer creditsCount, Integer allScore, Integer passScore) {

        CourseEntity courseEntity = new CourseEntity();
        courseEntity.setCourse_name(courseName);
        courseEntity.setCourse_code(courseCode);
        courseEntity.setSection_count(sectionCount);
        courseEntity.setCourse_time(courseTime);
        courseEntity.setMax_student_count(maxStudentCount);
        courseEntity.setCourse_profile(courseProfile);
        courseEntity.setTextbook(textbook);
        courseEntity.setCredits_count(creditsCount);
        courseEntity.setAll_score(allScore);
        courseEntity.setPass_score(passScore);

        // 根据教师ID获取教师信息
        ApiResponse apiResponse = JSON.parseObject(teacherApi.getTeacherInfo(teacherId), ApiResponse.class);
        if (!apiResponse.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())) {
            System.out.println(apiResponse);
            return 0;
        }
        TeacherDTO teacherDTO = JSON.parseObject(JSON.toJSONString(apiResponse.getData()), TeacherDTO.class);
        courseEntity.setTeacher_user_id(teacherId);
        courseEntity.setTeacher_realname(teacherDTO.getRealname());

        return courseMapper.addCourse(courseEntity);
    }

    @Override
    public List<CourseEntity> allCourses() {
        return courseMapper.allCourses();
    }
}
